<template>
    <el-card>
        <template #header>
            <span>成员管理</span>
        </template>
        <el-form :inline="true">
            <el-form-item><el-button type="primary" @click="dialogVisible = true">新增</el-button></el-form-item>
            <el-form-item label="用户名"> <el-input v-model="searchForm.username" clearable /> </el-form-item>
            <el-form-item label="角色">
                <el-select v-model="searchForm.rid" placeholder="请选择角色" clearable>
                    <el-option v-for="item in roleData" :label="item.rolename" :value="item.id" :key="item.id" />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button :icon="Search" square type="primary" @click="getData" />
            </el-form-item>
        </el-form>
        <el-table :data="managerData">
            <el-table-column label="ID" prop="id" align="center"></el-table-column>
            <el-table-column label="成员名称" prop="username" align="center"></el-table-column>
            <el-table-column label="角色名称" prop="rolename" align="center"></el-table-column>
            <el-table-column label="操作" align="center" justify="middle">
                <template #default="{ row }">
                    <el-button type="warning" size="small" @click="handleEdit(row.id)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDelete(row.id)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-row justify="center">
            <el-pagination background layout="prev, pager, next" :total="total" v-model:page-size="pageSize"
                v-model:current-page="page" @current-change="getData" />
        </el-row>
        <el-dialog v-model="dialogVisible" title="新增成员" width="500">
            <el-form label-width="80px" :model="addForm" :rules="rules" ref="formIns">
                <el-form-item label="成员名称" prop="username">
                    <el-input v-model="addForm.username"></el-input>
                </el-form-item>
                <el-form-item label="角色" prop="rid">
                    <el-select v-model="addForm.rid" placeholder="请选择角色" clearable>
                        <el-option v-for="item in roleData" :label="item.rolename" :value="item.id" :key="item.id" />
                    </el-select>
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="dialogVisible = false">取消</el-button>
                    <el-button type="primary" @click="handleSubmit">
                        提交
                    </el-button>
                </div>
            </template>
        </el-dialog>
        <el-dialog v-model="dialogVisible2" title="更新成员" width="500">
            <el-form label-width="80px" :model="editForm" :rules="rules" ref="formIns2">
                <el-form-item label="成员名" prop="username">
                    <el-input v-model="editForm.username"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="editForm.password"></el-input>
                </el-form-item>
                <el-form-item label="角色" prop="rid">
                    <el-select v-model="editForm.rid" clearable>
                        <el-option v-for="item in roleData" :label="item.rolename" :value="item.id" :key="item.id" />
                    </el-select>
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="dialogVisible2 = false">取消</el-button>
                    <el-button type="primary" @click="handleSubmit2">
                        提交
                    </el-button>
                </div>
            </template>
        </el-dialog>
    </el-card>
</template>
<script setup lang="ts">
import { ref, reactive, onMounted } from "vue";
import { getManager, getRole, addManager, delManager, oneManager, edtManager } from "@/api/api";
import { Search } from '@element-plus/icons-vue';
import type { FormInstance, FormRules } from "element-plus"
import { ElMessage, ElMessageBox } from 'element-plus'
//查询
const managerData = ref<ManagerData[]>([]);
const pageSize = ref(5);
const total = ref(0);
const page = ref(1);
const getData = async () => {
    const option: ManagerQuery = {
        page: page.value,
        limit: pageSize.value
    }
    if (searchForm.username) {
        option.username = searchForm.username;
    }
    if (searchForm.rid) {
        option.rid = searchForm.rid;
    }
    const res = await getManager(option);
    managerData.value = res.data.data;
    total.value = res.data.total;
}
const searchForm = reactive({
    username: "",
    rid: null
})
const roleData = ref<RoleData[]>();
onMounted(async () => {
    getData();
    const res = await getRole({ page: 1, limit: 20 });
    roleData.value = res.data.data;
})
//新增
const dialogVisible = ref(false);
const addForm = reactive<ManagerForm>({
    username: "",
    rid: null
})
const rules: FormRules = {
    username: { required: true, message: "请输入用户名", trigger: "blur" },
    rid: { required: true, message: "请选择角色", trigger: "blur" }
}
const formIns = ref<FormInstance>();
const handleSubmit = () => {
    formIns.value?.validate(async (v) => {
        if (v) {
            const res = await addManager(addForm);
            if (res.data.code === 200) {
                ElMessage.success("新增成功");
                dialogVisible.value = false;
                formIns.value?.resetFields();
                getData();
            } else {
                ElMessage.error(res.data.msg);
            }
        }
    })
}
//删除
const handleDelete = async (id: number) => {
    try {
        await ElMessageBox.confirm(
            '确认删除当前角色吗?确认继续',
            '警告',
            {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning',
            }
        );
        const res = await delManager(id);
        if (res.data.code === 200) {
            ElMessage.success("删除成功");
            getData();
        } else {
            ElMessage.error("删除失败");
        }
    } catch (e) {
        ElMessage.info("取消删除")
    }
}
//编辑
const dialogVisible2 = ref(false);
const editForm = ref<ManagerInfo>({
    id: 0,
    username: "",
    password: "",
    rid: null
});
const formIns2 = ref<FormInstance>();
const handleEdit = async (id: number) => {
    dialogVisible2.value = true;
    const res = await oneManager(id);
    editForm.value = res.data.data;
    editForm.value.password = "";
}
const handleSubmit2 = async () => {
    try {
        await formIns2.value?.validate();
        const res = await edtManager(editForm.value.id, editForm.value);
        if (res.data.code === 200) {
            ElMessage.success("更新成功");
            dialogVisible2.value = false;
            getData();
        } else {
            ElMessage.error(res.data.msg);
        }
    } catch (error) {
        throw error;
    }
}
</script>
<style scoped>
.el-row {
    height: 60px;
}

.el-select {
    width: 120px;
}
</style>
